package model

import (
	"errors"
	"github.com/doujunyu/gogo/sql_aid"
)

const TableByMusicLike = "self_music_like"

func MusicLikeByList(userId int64) ([]map[string]interface{},error) {
	musicLikeSql, args := sql_aid.PgTable(TableByMusicLike).Where("user_id = ?",userId).ToSql()
	musicLikeData, err := sql_aid.DataToMap(PgSql.Query(musicLikeSql, args...))
	if err != nil {
		return []map[string]interface{}{},errors.New("内部错误")
	}
	if len(musicLikeData) >= 0 {
		return []map[string]interface{}{},nil
	}
	//musicIds := util.ArrayField(&musicLikeData,"music_id")



	//Msql, args := sql_aid.PgTable(TableByMusic).WhereIn("id",musicIds...).ToSql()
	//musicData, err := sql_aid.DataToMap(PgSql.Query(sql, args...))
	//if err != nil {
	//	return err, []map[string]interface{}{}
	//}
	//http := os.Getenv("SERVER_HTTP")
	//for _, datum := range musicData {
	//	datum["file_url"] = util.IsPicturePath(datum["file_url"].(string),http)
	//}
	return  musicLikeData,nil
}

// MusicLikeByJoin 用户增加喜欢
func MusicLikeByJoin(userId int64, musicId int64) error {
	insert := make(map[string]interface{})
	insert["user_id"] = userId
	insert["music_id"] = musicId
	sql, args := sql_aid.PgTable(TableByMusicLike).InsertByMap(insert)
	_, err := PgSql.Exec(sql, args...)
	if err != nil {
		return err
	}
	return nil
}

// MusicLikeByExit 用户删除喜欢
func MusicLikeByExit(userId int64, musicLikeId int64) error {
	sql, args := sql_aid.PgTable(TableByMusicLike).Where("id = ?", musicLikeId).Where("user_id = ", userId).Delete()
	_, err := PgSql.Exec(sql, args...)
	if err != nil {
		return err
	}
	return nil
}
